package com.micmac.main.mainmodel
{
	
	/*
	
	Author : Roy Raul.
	
	Component : StartupMonitorModel             
	
	Version History         : 0.1 Date Jul-20-2011
	
	Current Version         : 0.1 Date Jul-20-2011
	
	Build Date              : Apr-14-2010
	
	*/
	
	
	import com.AppConst;
	import com.micmac.main.events.SplashViewEvent;
	import com.micmac.main.mainmodel.interfaces.IStartupModel;
	import com.micmac.main.mainmodel.vo.ResourceVO;
	
	import flash.events.Event;
	
	import org.robotlegs.mvcs.Actor;
	
	import spark.components.Application;

	
	public class StartupMonitorModel extends Actor implements IStartupModel
	{
	
		// array for store all the resource to read
		private var resourceList		:Array;
		// total resource to read 
		private var totalReources		:int 		= 0;
		// number of loaded resources
		private var loadedReources		:int 		= 0;
		
		private var _loadingPercentage 	:Number		= 0;
		
		public function StartupMonitorModel():void
		{
			
			resourceList = new Array();
			super();
			
		}
		
		/**
		 * Add a resource to load
		 * 
		 * @param name proxy name
		 * @param blockChain if the load process is stopped until this resource is loaded
		 */
		
		public function addResource( startupService:*, blockChain:Boolean = false ):void{
			this.resourceList.push( new ResourceVO( startupService, blockChain ) );
		}
		
		/**
		 * Start to read all resources
		 */
		
		public function loadResources():void{
			
			
			for( var i:int = 0; i < this.resourceList.length; i++){
				var r:ResourceVO = this.resourceList[i] as ResourceVO;
				if ( !r.loaded ){
					var startupService:* = r.startupService;
					
					startupService.load();
					// check if the loading process must be stopped until the resource is loaded
					if ( r.blockChain ){
						break;
					}
				}
			}
		}
		
		/**
		 * The resource is loaded, update the state anche check if the loading process is completed
		 * 
		 * @param name proxy name
		 */
		
		public function resourceComplete( startupService:* ):void{
			for( var i:int = 0; i < this.resourceList.length; i++){
				var r:ResourceVO = this.resourceList[i] as ResourceVO;
				if ( r.startupService == startupService ){
					if(!r.loaded){
						r.loaded = true;
						this.loadedReources ++;
						
						_loadingPercentage = Math.round((this.loadedReources * 100 ) / this.resourceList.length);
						
						dispatch(new SplashViewEvent(SplashViewEvent.LOADING_PROGRESS, _loadingPercentage));
						// check if the process is completed
						// if is not completed and the resources have blocked the process chain
						// continue to read the other resources
						if ( !this.checkResources() && r.blockChain )
						{
							this.loadResources();
						}
						break;
					}
				}
			}
		}
		
		/**
		 * Check if the loading process is completed
		 * 
		 * @return boolean process is completed
		 */
		
		private function checkResources():Boolean{
			for( var i:int = 0; i < this.resourceList.length; i++){
				var r:ResourceVO = this.resourceList[i] as ResourceVO;
				if ( !r.loaded ){
					return false;
				}
			}
			
			dispatch(new Event(AppConst.RESOURCE_LOADING_COMPLETE));
			
			return true;
		}
		
	}
}